package com.sunop.panticommon.config;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

@Configuration
public class HttpClientConfig {

    @Bean
    public CloseableHttpClient httpClient() {
        PoolingHttpClientConnectionManager connectionManager = 
            new PoolingHttpClientConnectionManager(
                30, TimeUnit.SECONDS  // 连接存活时间
            );
        connectionManager.setMaxTotal(200);      // 最大连接数
        connectionManager.setDefaultMaxPerRoute(50); // 每个路由最大连接数

        return HttpClients.custom()
                .setConnectionManager(connectionManager)
                .evictIdleConnections(30, TimeUnit.SECONDS) // 清理空闲连接
                .build();
    }
}